﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web.Mvc;
using InfoTech.Core.Service;
using InfoTech.LanTranh.MucVu.Bussiness;

namespace InfoTech.LanTranh.MucVu.Service
{
    public class TINHTHANHService : BaseService
    {

        public static bool Check_Insert_TINHTHANH(TINHTHANH tinhthanh)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<TINHTHANH>().FirstOrDefault(c => (c.TENTINHTHANH.ToUpper() == tinhthanh.TENTINHTHANH.ToUpper() ||
                c.MABUUCHINH.ToUpper() == tinhthanh.MABUUCHINH.ToUpper()) && c.MAQUOCGIA == tinhthanh.MAQUOCGIA) == null;
            return result;
        }

        public static bool Check_Update_TINHTHANH(TINHTHANH tinhthanh)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<TINHTHANH>().FirstOrDefault(c => (c.TENTINHTHANH.ToUpper() == tinhthanh.TENTINHTHANH.ToUpper() ||
                c.MABUUCHINH.ToUpper() == tinhthanh.MABUUCHINH.ToUpper()) &&
                c.MAQUOCGIA == tinhthanh.MAQUOCGIA && c.MATINHTHANH != tinhthanh.MATINHTHANH) == null;
            return result;
        }

        public static bool Check_Delete_TINHTHANH(string[] strings)
        {
            bool result = true;
            DataContext dataContext = CreateDataContext();
            result &= dataContext.GetTable<QUANHUYEN>().FirstOrDefault(c => strings.Contains(c.MATINHTHANH + "")) == null;
            return result;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_Country()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Tỉnh Thành} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<QUOCGIA>().Join(dataContext.GetTable<TINHTHANH>(), c => c.MAQUOCGIA, v => v.MAQUOCGIA, (c, v) => new { v.MATINHTHANH, TENTINHTHANH = v.TENTINHTHANH + " - " + c.TENQUOCGIA }).OrderBy(c => c.TENTINHTHANH), "MATINHTHANH", "TENTINHTHANH"));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_Country_Data()
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Tỉnh Thành} ---", Value = "" });
            list.AddRange(dataContext.GetTable<QUOCGIA>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false)
                .Join(dataContext.GetTable<TINHTHANH>().Where(c => c.ISDATA.HasValue ? c.ISDATA.Value : false)
                      , c => c.MAQUOCGIA, v => v.MAQUOCGIA,
                      (c, v) => new { v.MATINHTHANH, TENTINHTHANH = v.TENTINHTHANH + " - " + c.TENQUOCGIA, v.ISDEFAULT })
                .OrderBy(c => c.TENTINHTHANH).Select(
                    c =>
                    new SelectListItem() { Text = c.TENTINHTHANH, Value = c.MATINHTHANH + "", Selected = c.ISDEFAULT ?? false }));
            return list;
        }

        public static IEnumerable<SelectListItem> Get_List_Join_Country(string id)
        {
            DataContext dataContext = CreateDataContext();
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "--- {Chọn Tỉnh Thành} ---", Value = "" });
            list.AddRange(new SelectList(dataContext.GetTable<QUOCGIA>().Where(c => c.MAQUOCGIA + "" == id).Join(dataContext.GetTable<TINHTHANH>(), c => c.MAQUOCGIA, v => v.MAQUOCGIA, (c, v) => new { v.MATINHTHANH, v.TENTINHTHANH }).OrderBy(c => c.TENTINHTHANH), "MATINHTHANH", "TENTINHTHANH"));
            return list;
        }
    }
}
